const id = location.search.substring(1)


const renderShow = async () => {
  const { data: res } = await axios.get(`/index/show?id=${id}`)
  console.log(res)
  const info = res.data.info
  // id = info.id
  // console.log(id)

  $('.info').html(`
  <header class="article-header">
          <!-- 面包屑 -->
          <div class="breadcrumbs">
            <span class="text-muted">当前位置：</span>
            <a href="">优点达资讯</a>
            <small>></small>
            <a href="">${info.catename}</a> <small>></small>
            <span class="text-muted">正文</span>
          </div>
          <!-- 标题 -->
          <h1 class="article-title"><a href="">${info.title}</a></h1>
          <!-- 标题描述 -->
          <ul class="article-meta">
            <li><a href="">${info.author}</a> 发布于 ${formatTime(info.create_date)}</li>
            <li>分类：<a href="#/" rel="category tag">${info.catename}</a></li>
            <li><span class="post-views">阅读(${info.click})</span></li>
            <li>评论(1)</li>
            <li><a href="javascript:;" rel="category tag" id="collect" class=${info.isCollect ? 'collect' : ''}>${info.isCollect ? '已收藏' : '点击收藏'}</a></li>
          </ul>
        </header>
        <!-- 广告位 -->

        <!-- 文章正文 -->
        ${info.content}
        <!-- 点赞 -->
        <div class="article-social">
          <a href="javascript:;" class="action ${info.isLike ? 'action-like' : 'quxiao'}" id="dianzan">
            <i class="glyphicon glyphicon-thumbs-up"></i>${info.isLike ? "已点赞" : '点赞'}(<span>${info.lovenum ? info.isLike ? + info.lovenum : info.lovenum >= 1 ? info.lovenum - 1 : info.lovenum : 0}</span>)
          </a>
        </div>
  `)



  $('.article-nav-prev').html(res.data.prev.map(item => {
    return `
            <span>上一篇</span>
            <a href="show.html?${item.id}" rel="prev">${item.title}</a>
    `
  }).join(''))
  $('.article-nav-next').html(res.data.next.map(item => {
    return `
            <span>下一篇</span>
            <a href="show.html?${item.id}" rel="next">${item.title}</a>
    `
  }).join(''))


  $('.relates ul').html(res.data.recommend.map(item => {
    return `
            <li>
              <a target="_blank" href="#/">
                <span>
                  <img src="http://124.223.14.236:8060/${item.pic}" class="thumb" />
                </span>
                ${item.title}
              </a>
            </li>
    `
  }).join(''))
}
renderShow()
const layer = layui.layer


// 点击收藏
const options = {
  type: '',
  action: '',
  article_id: id
}
$('body').on('click', '#collect', async function () {

  options.type = 1
  options.action = $(this).hasClass('collect') ? 'del' : 'add'

  const { data: res } = await axios.get('/user/userDataHandle', { params: options }, { headers: token })
  // console.log(res);
  if (res.errno) return layer.msg(res.errmsg)
  renderShow()
})



// 点赞
$('body').on('click', '#dianzan', async function () {
  options.type = 2
  options.action = $(this).hasClass('action-like') ? 'del' : 'add'

  const { data: res } = await axios.get('/user/userDataHandle', { params: options }, { headers: token })
  // console.log(res);
  if (res.errno) return layer.msg(res.errmsg)
  renderShow()
})

